home *** CD-ROM | disk | FTP | other *** search
/ AMIGA-CD 2 / Amiga-CD - Volume 2.iso / ungepackte_daten / 1993 / 7 / 02 / tips & tricks / splitfile / splitfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-01  |  1.7 KB  |  63 lines

  1. /* SplitFile.c - von Christof Brühann
  2.  * Aufruf mit DICE: dcc SplitFile.c -o SplitFile */
  3. #include <dos/dos.h>
  4. #include <exec/memory.h>
  5. #define BUFFERSIZE 32000
  6. int main(long argc,char **argv)
  7. {
  8.  struct FileHandle *file_old,*file_new;
  9.  ULONG size_old,size_new,filenum,i,j;
  10.  UBYTE filename_new[255],ext[255],*buffer;
  11.  if (argc!=3) {
  12.   printf("\nAufruf: SplitFile <Datei> <Größe>\n\n");
  13.   return(0);
  14.  }
  15.  if(file_old=(struct FileHandle *)
  16.                   Open(argv[1],MODE_OLDFILE)) {
  17.   /* Quelldatei */
  18.   Seek(file_old,0,OFFSET_END);
  19.   /* Größe Quelldatei */
  20.   size_old=Seek(file_old,0,OFFSET_BEGINNING);
  21.   sscanf(argv[2],"%d",&size_new);
  22.   if(size_new<size_old && size_new!=0) {
  23.    /* Größenparameter korrekt? */
  24.    if(buffer=(UBYTE *)
  25.         AllocMem(BUFFERSIZE,MEMF_PUBLIC)) {
  26.     /* Puffer reservieren*/
  27.     for (filenum=1;filenum<size_old/size_new+2;
  28.                                     filenum++) {
  29.      /* Schleife für Erstellung der Teildateien */
  30.      sprintf(ext,"_%02d",filenum);
  31.      strcpy(filename_new,argv[1]);
  32.      strcat(filename_new,ext);
  33.      if (file_new=(struct FileHandle *)
  34.              Open(filename_new,MODE_NEWFILE)) {
  35.       /* Teildatei öffnen */
  36.       printf("Erstelle '%s'\n",filename_new);
  37.       i=0;
  38.       while (i<size_new) {
  39.        /* Teildatei mit Pufferung erstellen */
  40.        j=BUFFERSIZE;
  41.        if (size_new-i<BUFFERSIZE) j=size_new-i;
  42.        if (j=Read(file_old,buffer,j))
  43.          Write(file_new,buffer,j);
  44.        else {
  45.         filenum=-2;
  46.         break;
  47.        }
  48.        i+=BUFFERSIZE;
  49.       }
  50.       Close(file_new);
  51.      }
  52.      else break;
  53.     }
  54.     FreeMem(buffer,BUFFERSIZE);
  55.    }
  56.   }
  57.   Close(file_old);
  58.  }
  59.  else
  60.    printf("%s kann nicht geöffnet werden.",argv[1]);
  61.  return(0);
  62. }
  63.